package com.example.springboot011problemdetail.controller;

import com.example.springboot011problemdetail.Exception.BookNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
import org.springframework.web.ErrorResponse;
import org.springframework.web.ErrorResponseException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.net.URI;
import java.time.Instant;

/**
 * @description: TODO
 * @author: uu
 * @data: 2023/7/8 下午 02:46
 * @version: v1.0
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
/*    @ExceptionHandler(value = {BookNotFoundException.class})
    public ProblemDetail handlerBookNotFoundException(BookNotFoundException e)  {
        ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage());
        problemDetail.setType(URI.create("/api/probs/not-found"));
        problemDetail.setTitle("图书异常");
        // 自定义字段
        //时间戳
        problemDetail.setProperty("timeStamp", Instant.now());
        // 客服邮箱
        problemDetail.setProperty("客服邮箱", "cms@cc.com");
        return problemDetail;
    }*/

    // ErrorResponse
    @ExceptionHandler(value = {BookNotFoundException.class})
    public ErrorResponse handleException(BookNotFoundException e){
        ErrorResponse error = new ErrorResponseException(HttpStatus.NOT_FOUND,e);
        return error;
    }
}
